package view.components.list
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import utils.DisplayTool;
	
	import view.components.UIComponents;

	public class TileList extends Sprite
	{
		private var scrollPane:*;
		private var items:Array;
		private var itemLayer:Sprite;
		private var currentSelectedItems:Array;
		private var dWidth:int;
		private var dHeight:int;
		private var listItem:ITileListItem;
		private var isMultiSelect:Boolean;
		
		public function TileList(width:int,height:int,listItem:ITileListItem,isMultiSelect:Boolean = false)
		{
			dWidth = width;
			dHeight = height;
			this.isMultiSelect = isMultiSelect;
			this.listItem = listItem;
			init();
		}
		
		private function init():void
		{
			currentSelectedItems = [];
			items = [];
			itemLayer = new Sprite();
			scrollPane = UIComponents.getScrollPane(dWidth - 2,dHeight -1);
			scrollPane.x = 2;
			scrollPane.y = 1;
			addChild(scrollPane);
			scrollPane.source = itemLayer;
			
			
		}
		
		
		public function drawBg():void
		{
			with (this.graphics)
			{
				clear();
				lineStyle(1,0xAAAAAA,1);
				drawRoundRect(0,0,dWidth,dHeight,14,14);
			}
		}
		
		public function addItem(data:Object,isSelected:Boolean = false):void
		{
			if (data)
			{
				var listItem:ITileListItem = this.listItem.getInstance();
				listItem.update(data);
				listItem.self.x = 2;
				listItem.self.y = items.length * (listItem.self.height + 3) + 3 ;
				listItem.index = items.length;
				items.push(listItem);
				setItemListener(listItem);
				itemLayer.addChild(listItem.self);
				
				
				if (isSelected)
				{
					listItem.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
				}
				
				scrollPane.refreshPane();
			}
			
		}
		
		public function getItemByIndex(index:int):ITileListItem
		{
			if (index >= 0 && index < items.length)
			{
				return items[index];
			}
			return null;
		}
		
		public function removeItemAt(index:int):void
		{
			if (index >= 0 && index < items.length)
			{
				var listItem:ITileListItem;
				listItem = items.splice(index,1)[0];
				listItem.destroy();
				
				DisplayTool.removeChild(listItem.self.parent,listItem.self);
				
				for (var i:int=0; i<items.length; i++)
				{
					listItem = items[i];
					listItem.self.x = 2;
					listItem.self.y = i * (listItem.self.height + 3) + 3 ;
					listItem.index = i;
				}
				scrollPane.update();
			}
		}
		
		private function setItemListener(item:ITileListItem):void
		{
			item.addEventListener(MouseEvent.MOUSE_OVER,onOver);
			item.addEventListener(MouseEvent.MOUSE_OUT,onOut);
			item.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
		}
		
		private function removeItemListener(item:ITileListItem):void
		{
			item.removeEventListener(MouseEvent.MOUSE_OVER,onOver);
			item.removeEventListener(MouseEvent.MOUSE_OUT,onOut);
			item.removeEventListener(MouseEvent.MOUSE_DOWN,onDown);
		}
		
		private function onOver(event:MouseEvent):void
		{
			event.currentTarget.drawOverBg();
		}
		
		private function onOut(event:MouseEvent):void
		{
			event.currentTarget.drawOutBg();
		}
		
		private function onDown(event:MouseEvent):void
		{
			var item:ITileListItem = event.currentTarget as ITileListItem;
			if (isMultiSelect)
			{
				var index:int = isInSelectedItems(item);
				if (index >= 0)
				{
					item.selected = false;
					item.drawOutBg();
					currentSelectedItems.splice(index,1);
				}
				else
				{
					item.drawSelectBg();
					item.selected = true;
					addSelectedItem(item);
				}
			}
			else
			{
				clearSelectedItem();
				
				item.drawSelectBg();
				item.selected = true;
				addSelectedItem(item);
			}
		}
		
		private function addSelectedItem(item:ITileListItem):void
		{
			if (currentSelectedItems.indexOf(item) < 0)
			{
				currentSelectedItems.push(item);
			}
		}
		
		private function removeSeletedItem(item:ITileListItem):void
		{
			var index:int = currentSelectedItems.indexOf(item);
			if (index >= 0)
			{
				currentSelectedItems.splice(index,1);
			}
		}
		
		public function clearSelectedItem():void
		{
			var item:ITileListItem;
			for each (item in currentSelectedItems)
			{
				item.selected = false;
				item.drawOutBg();
			}
			currentSelectedItems.length = 0;
		}
		
		
		private function isInSelectedItems(item:ITileListItem):int
		{
			return currentSelectedItems.indexOf(item);
		}
		
		public function removeAll():void
		{
			currentSelectedItems.length = 0;
			for each (var item:ITileListItem in items)
			{
				removeItemListener(item);
				item.destroy();
			}
			items.length = 0;
			DisplayTool.removeAllChild(itemLayer);
			scrollPane.update();
		}
		
		public function get selectedItems():Array
		{
			return currentSelectedItems.concat();
		}
		
		public function get count():int
		{
			return items.length;
		}
		
		public function seachItem(property:String,value:*):ITileListItem
		{
			var item:ITileListItem;
			for (var i:int=0;i<items.length;i++)
			{
				item = items[i];
				if (item.data.@[property] == value)
				{
					return item;
				}
			}
			
			return null;
		}
		
		public function switchItemsState(state:int):void
		{
			for each (var item:ITileListItem in items)
			{
				item.switchState(state);
			}
		}

	}
}